import React from 'react';
import { BrowserRouter as Router, Routes, Route, Navigate } from 'react-router-dom';
import Login from '../pages/Login';
import DeviceManagement from '../pages/DeviceManagement';
import ProtectedRoute from '../components/ProtectedRoute';
import useAuthStore from '../store/authStore';

const AppRoutes = () => {
  const { isAuthenticated } = useAuthStore();

  return (
    <Router>
      <Routes>
        {/* 登录页面 */}
        <Route 
          path="/login" 
          element={
            isAuthenticated ? <Navigate to="/devices" replace /> : <Login />
          } 
        />
        
        {/* 根路径重定向 */}
        <Route 
          path="/" 
          element={
            isAuthenticated ? <Navigate to="/devices" replace /> : <Navigate to="/login" replace />
          } 
        />
        
        {/* 受保护的路由 */}
        <Route 
          path="/devices" 
          element={
            <ProtectedRoute>
              <DeviceManagement />
            </ProtectedRoute>
          } 
        />
        
        {/* 404 页面重定向 */}
        <Route path="*" element={<Navigate to="/devices" replace />} />
      </Routes>
    </Router>
  );
};

export default AppRoutes;